package com.jiyinit.web.core.common.logic;

import com.alibaba.fastjson.JSON;
import com.jiyinit.core.framework.base.entity.DataTransferObject;
import com.jiyinit.core.framework.base.entity.ResponseDto;
import com.jiyinit.core.framework.base.util.Check;
import com.jiyinit.core.framework.base.util.MessageSourceUtil;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Iterator;
import java.util.Set;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author lishaochuan
 * @version 1.0
 * @date 2017/4/18 15:22
 * @since 1.0
 */
@Component("core.paramCheckLogic")
public class ParamCheckLogic {

    @Resource(name = "core.validator")
    private Validator validator;

    /**
     * 校验对象参数，并依据validator校验
     *
     * @param t
     * @return
     */
    public <T> ValidateResult<T> checkParamValidate(T t) {
        ResponseDto dto;
        //参数是否为空
        if (Check.isNullObj(t)) {
            dto = ResponseDto.responseFail("参数为空");
            return new ValidateResult<T>(dto);
        }
        //validator校验
        dto = checkObjParamValidate(t);
        if (!Check.isNullObj(dto)) {
            return new ValidateResult<T>(dto);
        }
        return new ValidateResult<T>();
    }

    /**
     * 校验json参数，并依据validator校验
     *
     * @param jsonStr
     * @param clazz
     * @return
     */
    public <T> ValidateResult<T> checkParamValidate(String jsonStr, Class<T> clazz) {
        ResponseDto dto;
        //参数是否为空
        if (Check.isNullStr(jsonStr)) {
            dto = ResponseDto.responseFail("参数为空");
            return new ValidateResult<T>(dto);
        }
        //validator校验
        T t = JSON.parseObject(jsonStr, clazz);
        dto = checkObjParamValidate(t);
        if (!Check.isNullObj(dto)) {
            return new ValidateResult<T>(dto);
        }
        return new ValidateResult<T>(t);
    }

    /**
     * 对象校验，依据validator
     * use case
     * 派单前做的工单校验
     *
     * @param t
     * @param <T>
     * @return
     */
    public <T> ResponseDto checkObjParamValidate(T t) {
        //参数是否为空
        if (Check.isNullObj(t)) {
            ResponseDto.responseFail("参数为空");
        }
        //是否符合validator校验
        StringBuilder errorMsg = new StringBuilder();
        Set<ConstraintViolation<T>> constraintViolationSet = validator.validate(t);
        if (!Check.isNullCollection(constraintViolationSet)) {
            Iterator<ConstraintViolation<T>> iterator = constraintViolationSet.iterator();
            while (iterator.hasNext()) {
                ConstraintViolation<T> constraint = iterator.next();
                errorMsg.append(constraint.getMessage()).append(",");
            }
            //设置错误信息
            if (errorMsg.length() != 0) {
                errorMsg.deleteCharAt(errorMsg.length() - 1);
                return ResponseDto.responseFail(errorMsg.toString());
            }
        }
        return null;
    }
}
